﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NhanAi.Entity;
using NhanAi.DAO;
using NhanAi.DAO.Mix;
using Home.Utils;
using NhanAi.App_Code;
namespace NhanAi.Admin
{
    public partial class EditPage : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Authenticate();
           if (!IsPostBack)
            {
                int id = TypeWrapper.GetInt32(Request["id"]);
                PostModel daoPost = new PostModel();
                TaxonomyDao daoTax = new TaxonomyDao();
                Post p = daoPost.Read(id, NAConstants.ALL_LANGUAGE);
                hdnId.Value = id.ToString();
                txtTitle.Text = p.Title;
                txtTitleEn.Text = p.TitleEn;
                PostContent.Value = p.Content;
                PostContentEn.Value = p.ContentEn;
                txtName.Text = p.Name != null ? p.Name : "";
                cmbStatus.Items.Add(new ListItem("Nháp", NAConstants.POST_STA_DRAFT.ToString()));
                cmbStatus.Items.Add(new ListItem("Cho phép đăng", NAConstants.POST_STA_POST.ToString()));
                chkProtected.Checked = p.Protected;
               int index = 0;
               foreach (ListItem item in cmbStatus.Items)
               {
                   if (item.Value == p.Status.ToString())
                   {
                       cmbStatus.SelectedIndex = index;
                   }
                   index++;
               }
               
            }
           SetCurrentPage("Page");
        }
        public void btnUpdateClick(object sender, EventArgs e)
        {
            try
            {
                PostDao daoPost = new PostDao();
                PostModel modPost = new PostModel();
                int id = TypeWrapper.GetInt32(hdnId.Value);
                Post p = daoPost.Read(id, NAConstants.ALL_LANGUAGE);

                p.Title = txtTitle.Text;
                p.TitleEn = txtTitleEn.Text;
                p.Content = PostContent.Value;
                p.ContentEn = PostContentEn.Value;
                p.Status = int.Parse(cmbStatus.SelectedItem.Value);
               
                p.ModifiedDate = DateTime.Now;
                p.Protected = chkProtected.Checked;
                p.Guid = "";
                p.Name = txtName.Text;
                p.Excerpt = StringUltility.GetExcerpt(p.Content,NAConstants.EXCERPT_LEN);
                p.ExcerptEn = StringUltility.GetExcerpt(p.ContentEn,NAConstants.EXCERPT_LEN);
              
                modPost.Update(p);
            }
            catch (Exception ex)
            {
                SetMessage("Đã xảy ra lỗi","");
            }
            SetMessage("Cập nhật thành công", "");
        }
      
    }
}